package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class KeXueWenKu3 {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "pgv_pvid=9136866880; __qc_wId=740; JSESSIONID=2D0747BECBA6CBBE79EB7155C16F7849";
    public static final String ORIGIN = "http://book.sciencereading.cn";
    public static final String REFERER = "http://book.sciencereading.cn/shop/book/Booksimple/list.do";
    public static final String URL_PRE = "http://book.sciencereading.cn";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum =0;
        int totalPage = 1092;
        int pageSize = 50;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://book.sciencereading.cn/shop/book/Booksimple/list.do";
            Map<String, Object> parameter = MapUtil.newHashMap(4);
            parameter.put("pageSplit.nowPageNumber", i);
            parameter.put("showQueryModel.keywords1", "9787030");
            parameter.put("pageSplit.showRow", pageSize);
            parameter.put("showQueryModel.bookOrderColumn", "booksimple.publishDate desc");
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            while (result.contains("页面没有找到")) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(url, headers, parameter);
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Elements bookContentList = htmlDoc.select(".book_list_container_right .book_content");
                if (bookContentList != null && bookContentList.size() > 0) {
                    List<CxgBook> listBook = new ArrayList<>();
                    for (Element element : bookContentList) {
                        CxgBook bookEntity = new CxgBook();
                        Element imgElement = element.select(".img-responsive").first();
                        if (imgElement != null) {
                            String srcStr = imgElement.attr("src");
                            String cover = URL_PRE + srcStr;
                            bookEntity.setCover(cover);
                        }
                        Element titleElement = element.select(".book_detail_title a").first();
                        if (titleElement != null) {
                            Element textElement = titleElement.select(".kc_title").first();
                            if (textElement != null) {
                                String text = textElement.text();
                                bookEntity.setBookName(text);
                            }
                            String href = titleElement.attr("href");
                            String[] strings = href.split("id=");
                            String pkId = strings[1];
                            href = URL_PRE + href;
                            bookEntity.setDigitalUri(href);
                            bookEntity.setPkId(pkId);
                        }
                        Element authorElement = element.select(".book_detail_info .kc_author").first();
                        if (authorElement != null) {
                            String author = authorElement.text();
                            bookEntity.setAuthor(author);
                        }
                        Element isbnElement = element.select(".book_detail_info .kc_ISBN").first();
                        if (isbnElement != null) {
                            String isbn = isbnElement.text();
                            bookEntity.setBookIsbn(isbn);
                        }
                        Element briefElement = element.select(".book_detail_info .kc_introduction").first();
                        if (briefElement != null) {
                            String brief = briefElement.text();
                            bookEntity.setBrief(brief);
                        }
                        Element pubDateElement = element.select(".book_detail_info .col-lg-6 .keyContent").last();
                        if (pubDateElement != null) {
                            String pubDateStr = pubDateElement.text();
                            if (StrUtil.isNotBlank(pubDateStr)) {
                                pubDateStr = pubDateStr + "-01 00:00:00";
                                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                                LocalDateTime localDateTime = LocalDateTime.parse(pubDateStr, dtf);
                                bookEntity.setPubDate(localDateTime);
                            }
                        }
                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("科学出版社");
                        bookEntity.setStatus(1);
                        bookEntity.setPressId(4);
                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(Integer numId) throws Exception {
        CxgBook bookEntity = null;
        String url = "https://lib-nuanxin.wqxuetang.com/v1/book/initbook";
        Map<String, Object> query = MapUtil.newHashMap(2);
        query.put("bid", numId);
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Origin", ORIGIN);
        headers.put("Referer", REFERER);
        String result = HttpUtil.doGet(url, headers, query);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode != null && errcode == 0) {
            JSONObject entityJsonObject = jsonObject.getJSONObject("data");
            bookEntity = new CxgBook();
            bookEntity.setPkId(entityJsonObject.getString("bid"));
            bookEntity.setBookIsbn(entityJsonObject.getString("isbn"));
            bookEntity.setAuthor(entityJsonObject.getString("author"));
            String bookName = entityJsonObject.getString("name");
            bookEntity.setBookName(bookName);
            log.info(bookName);
            bookEntity.setCover(entityJsonObject.getString("coverurl"));
            bookEntity.setBrief(entityJsonObject.getString("descript"));
            bookEntity.setCatalog(entityJsonObject.getString("catalog"));
            String truePages = entityJsonObject.getString("true_pages");
            int pageNum = 0;
            if (StrUtil.isNotBlank(truePages)) {
                pageNum = Integer.parseInt(getNumbers(truePages));
            }
            bookEntity.setPageNum(pageNum);
            String words = entityJsonObject.getString("words");
            int wordNum = 0;
            if (StrUtil.isNotBlank(words)) {
                wordNum = Integer.parseInt(getNumbers(words));
            }
            bookEntity.setWordNum(wordNum);
            String pubDate = entityJsonObject.getString("date");
            if (StrUtil.isNotBlank(pubDate)) {
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss");
                LocalDateTime localDateTime = LocalDateTime.parse(pubDate, dtf);
                bookEntity.setPubDate(localDateTime);
            }
            bookEntity.setPrice(entityJsonObject.getBigDecimal("price"));
            bookEntity.setDigitalUri("https://lib-nuanxin.wqxuetang.com/read/pdf/" + numId);

        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
